package betting.manager;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.io.IOException;
import java.util.ArrayList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import betting.db.entitites.BasketRow;
import betting.db.entitites.SoccerRow;
import betting.db.entitites.Strings;

import java.util.Calendar;


public class GetScores {

	private final static String nicknames[] = basket_NBA_teamsInizialization();
	private final static String months[] = monthsInizialization();
	private final static int THREE_EVENT_NUMBER = 3;
	private static final String QUOTE_CLASS = "eventprice";
	private static final String BASKET_NBA_MATCH_CLASS = "CentrePad";
	private static final String SOCCER_MATCH_CLASS = "LeftPad";
	
	
	
	
	public static ArrayList<SoccerRow> getTodaySoccerMatches(String source){
		
		try {
			  Document doc = Jsoup.connect(source).get();  
			  
			  Elements quotes = doc.getElementsByClass(QUOTE_CLASS);
			  Elements matches = doc.getElementsByClass(SOCCER_MATCH_CLASS);
			  
			  ArrayList<SoccerRow> listMatches = new ArrayList<SoccerRow>();
			  ArrayList<String[]> total = new ArrayList<String[]>();		  

			  
			  // j = date, j+1 = hour, j+2 = match
			  for(int j= 2; j < matches.size(); j=j+3){
				  
				  String[] match  = new String[3];
				  String dateMatch = matches.get(j).text();
				  dateMatch = convertMonths(dateMatch);
				  
				  //if there are today matches
				  if(dateMatch.equals("")) {
					  String this_month  = "";
					  if((Calendar.getInstance().get(Calendar.MONTH)+1)<10)
						  this_month = "0" + String.valueOf(Calendar.getInstance().get(Calendar.MONTH)+1);
					  else this_month = String.valueOf(Calendar.getInstance().get(Calendar.MONTH)+1);
					  dateMatch = dateMatch.concat(String.valueOf(String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH)))+"/"+this_month);
				  }
				  
				  //replace of spaces in match date
				  dateMatch = dateMatch.replaceAll("\\p{Z}", "");
				  
				  //adding year to the match date
				  dateMatch = dateMatch.concat("/"+String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
				  
				  String teamlist = matches.get(j+2).text();
				  //replace of spaces in matches
				  teamlist = teamlist.replaceAll("\\p{Z}", "");
				  //replace of "?" characters
				  String[] teams = teamlist.split("\\W");
				  
				  match[0] = dateMatch;
				  match[1] = teams[0];
				  if(teams[1]!=null)
					  match[2] = teams[1];
				  
				  total.add(match);
				  
			  }
			  
			  for(int w = 0; w < total.size(); w++) {
				  
				  SoccerRow row = new SoccerRow();
				  row.setDate(total.get(w)[0]);
				  row.setHome(total.get(w)[1]);
				  row.setAway(total.get(w)[2]);
				  int y = THREE_EVENT_NUMBER * w;
				  row.setQuota1(Float.parseFloat(quotes.get(y).text()));
				  row.setQuotaX(Float.parseFloat(quotes.get(y+1).text()));
				  row.setQuota2(Float.parseFloat(quotes.get(y+2).text()));
				  row.setLeague("A");
				  listMatches.add(row);
				  
				  
				  System.out.print(listMatches.get(w).getHome()+ " ");
				  System.out.print(listMatches.get(w).getAway()+ " ");
				  System.out.print(listMatches.get(w).getQuota1()+ " ");
				  System.out.print(listMatches.get(w).getQuotaX()+ " ");
				  System.out.print(listMatches.get(w).getQuota2()+ " ");
				  System.out.println(listMatches.get(w).getDate());
			  }
			  
			  return listMatches;
			  
		    } catch (IOException ex) {
		    	return null;
		    }
		
		
	}
	
	
	public static ArrayList<BasketRow> getTodayNBAMatches(String source){
		try {
			  Document doc = Jsoup.connect(source).get();  
			  
			  Elements quotes = doc.getElementsByClass(QUOTE_CLASS);
			  Elements matches = doc.getElementsByClass(BASKET_NBA_MATCH_CLASS);
			  
			  ArrayList<BasketRow> list = new ArrayList<BasketRow>();
			  ArrayList<String[]> total = new ArrayList<String[]>();
			  
			  for(int j= 0; j < matches.size(); j++){
				  
				  String teamlist = matches.get(j).text();
				  
				  teamlist = removeNbaNicknames(teamlist);
						  
				  teamlist = teamlist.replaceAll("\\p{Z}", "");
				  String[] match = teamlist.split("\\W");
			
				  total.add(match);
			  }
			  
			  for(int w = 0; w < total.size(); w++) {
				  
				  BasketRow row = new BasketRow();
				  row.setHome(total.get(w)[0]);
				  row.setAway(total.get(w)[1]);
				  int y = 2 * w;
				  row.setQuota1(Float.parseFloat(quotes.get(y).text()));
				  row.setQuota2(Float.parseFloat(quotes.get(y+1).text()));
				  Calendar now = Calendar.getInstance();
				  DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
				  row.setDate(format.format(now.getTime()));
				  row.setPeriodo("RS");
				  list.add(row);
				  
				  
				  System.out.print(list.get(w).getHome()+ " ");
				  System.out.print(list.get(w).getAway()+ " ");
				  System.out.print(list.get(w).getQuota1()+ " ");
				  System.out.print(list.get(w).getQuota2()+ " ");
				  System.out.println(list.get(w).getDate());
			  }
			  
			  return list;
			  
		    } catch (IOException ex) {
		    	return null;
		    }
		
		
	}
	
	
	public static String[] basket_NBA_teamsInizialization(){
		
		String[] nicknames = new String[Strings.TOTAL_NBA_TEAMS];
		  nicknames[0] = Strings.BASKET_NBA_NETS;
		  nicknames[1] = Strings.BASKET_NBA_BLAZERS;
		  nicknames[2] = Strings.BASKET_NBA_BOBCATS;
		  nicknames[3] = Strings.BASKET_NBA_BUCKS;
		  nicknames[4] = Strings.BASKET_NBA_BULLS;
		  nicknames[5] = Strings.BASKET_NBA_CAVS;
		  nicknames[6] = Strings.BASKET_NBA_CELTICS;
		  nicknames[7] = Strings.BASKET_NBA_GRIZZLIES;
		  nicknames[8] = Strings.BASKET_NBA_HAWKS;
		  nicknames[9] = Strings.BASKET_NBA_HEAT;
		  nicknames[10] = Strings.BASKET_NBA_JAZZ;
		  nicknames[11] = Strings.BASKET_NBA_KINGS;
		  nicknames[12] = Strings.BASKET_NBA_KNICKS;
		  nicknames[13] = Strings.BASKET_NBA_MAGIC;
		  nicknames[14] = Strings.BASKET_NBA_MAVS;
		  nicknames[15] = Strings.BASKET_NBA_NUGGETS;
		  nicknames[16] = Strings.BASKET_NBA_PACERS;
		  nicknames[17] = Strings.BASKET_NBA_PELICANS;
		  nicknames[18] = Strings.BASKET_NBA_PISTONS;
		  nicknames[19] = Strings.BASKET_NBA_RAPTORS;
		  nicknames[20] = Strings.BASKET_NBA_ROCKETS;
		  nicknames[21] = Strings.BASKET_NBA_SIXERS;
		  nicknames[22] = Strings.BASKET_NBA_SPURS;
		  nicknames[23] = Strings.BASKET_NBA_SUNS;
		  nicknames[24] = Strings.BASKET_NBA_THUNDER;
		  nicknames[25] = Strings.BASKET_NBA_TWOLVES;
		  nicknames[26] = Strings.BASKET_NBA_WARRIORS;
		  nicknames[27] = Strings.BASKET_NBA_WIZARDS;
		  nicknames[28] = Strings.BASKET_NBA_CLIPPERS;
		  nicknames[29] = Strings.BASKET_NBA_LAKERS;
		  return nicknames;
	}
	
	public static String[] monthsInizialization(){
		
		String[] months = new String[Strings.TOTAL_MONTHS];
		months[0] = Strings.JANUARY;
		months[1] = Strings.FEBRUARY;
		months[2] = Strings.MARCH;
		months[3] = Strings.APRIL;
		months[4] = Strings.MAY;
		months[5] = Strings.JUNE;
		months[6] = Strings.JULY;
		months[7] = Strings.AUGUST;
		months[8] = Strings.SEPTEMBER;
		months[9] = Strings.OCTOBER;
		months[10] = Strings.NOVEMBER;
		months[11] = Strings.DECEMBER;
		
		return months;
	}
	public static void printString(Object[] object){
		for(int h = 0; h < object.length ; h++)
			  System.out.println(object[h]);
		
	}
	
	public static String removeNbaNicknames(String teamlist){
		for(int w = 0; w < Strings.TOTAL_NBA_NICKNAMES; w++)
			   teamlist = teamlist.replace(nicknames[w],"");
		return teamlist;
	}
	
	public static String convertMonths(String month){
		for(int w = 1; w <= Strings.TOTAL_MONTHS; w++){
				if(w < 10) month = month.replace(months[w-1],"/0"+String.valueOf(w));
				else month = month.replace(months[w-1],"/"+String.valueOf(w));
			  	}
		return month;
	}
}
